package com.hp.system.utils;

import com.alibaba.fastjson.JSONObject;
import com.hp.system.domain.IdentifyInfo;
import com.hp.system.domain.Timeval;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JSONInfoUtil {
    public static StringBuffer getInfo(HttpServletRequest request){
        StringBuffer info = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine())!=null)
                info.append(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }

//    public static IdentifyInfo getResultByJson(JSONObject json){
//        JSONObject alarmInfoPlate = json.getJSONObject("AlarmInfoPlate");
//        JSONObject result = alarmInfoPlate.getJSONObject("result");
//        JSONObject plateResult = result.getJSONObject("PlateResult");
//        String serialno = alarmInfoPlate.getString("serialno");
//        Integer colorType = plateResult.getInteger("colorType");
//        String license = plateResult.getString("license");
//        JSONObject timeStamp = plateResult.getJSONObject("timeStamp");
//        JSONObject timeval = timeStamp.getJSONObject("Timeval");
//        Integer year = timeval.getInteger("decyear");
//        Integer month = timeval.getInteger("decmon");
//        Integer day = timeval.getInteger("decday");
//        Integer hour = timeval.getInteger("dechour");
//        Integer minute = timeval.getInteger("decmin");
//        Integer second = timeval.getInteger("decsec");
//        IdentifyInfo identifyInfo = new IdentifyInfo(colorType,license,year,month,day,hour,minute,second,serialno);
//        return identifyInfo;
//    }

    public static String timeFormate(String time){
        StringBuffer newTime = new StringBuffer("0");
        if (time.length()==1){
            return newTime.append(time).toString();
        }else
            return time;
    }

    public static String getTime(Timeval timeval){
        String year = timeval.getDecyear().toString();
        String month = timeFormate(timeval.getDecmon().toString());
        String day = timeFormate(timeval.getDecday().toString());
        String hour = timeFormate(timeval.getDechour().toString());
        String minute = timeFormate(timeval.getDecmin().toString());
        String second = timeFormate(timeval.getDecsec().toString());
        StringBuffer timeStr = new StringBuffer(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
        return timeStr.toString();
    }
    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(date);
    }
}
